<?php

class AjaxController  extends Zend_Controller_Action{

    public function init()
    {
        $this->getResponse()->setHeader('Cache-Control', 'no-cache, must-revalidate');
        $this->getResponse()->setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        $this->getResponse()->sendHeaders();
    }

    public function productsAction() {
		

        $categoryId = (int) $this->_getParam('categoryId');

		$productData = array();

        $catalogModel = new Cms_Model_Catalog();

		$results = $catalogModel->getProductsByCategory($categoryId, array('sort'=>'name'));

		foreach ($results as $result) {
			$productData[] = array(
				'productId' => $result->productId,
				'name'       => $result->name
			);
		}

		$this->_helper->json($productData);
	}

    public function productAction() {
		
                $id = $this->_getParam('id');

                $catalogModel = new Cms_Model_Catalog();
                $productInfo = null;
                $message = array();
                
                if(null !== $catalogModel->getProductById($id)){
                    $productInfo = $catalogModel->getProductById($id);
                }
                
                if(null !== $productInfo){
                    $message['productId'] = $productInfo->productId;
                    $message['name'] = $productInfo->name;
                }
                
		$this->_helper->json($message);
	}

}